package com.gljx.web.controller;

import com.gljx.constant.ActivitiKey;
import com.gljx.util.*;
import com.gljx.web.Entity.*;
import com.gljx.web.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
@RequestMapping("/")
public class SimulationQuargeController {
	@Autowired
	private SimulationQuargeService simulationQuargeService;
	
	@Autowired
	private SimulationQuargentryService simulationQuargentryService;
	
	//自动注入
	@Autowired
	private DepartmentService departmentService;

	@Autowired
	private CustomLogService customLogService;
	
	@Autowired
	private StudentService studentService;

	//自定义流程工具
	private Axamine axamine = new Axamine();

	//导入activiti工具
	ActitiviUtil activitiUtil = new ActitiviUtil();
	/**
	 * 根据组织查询模拟考合格登记
	 * 
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/querySimulationQuarge/look.action")
	public String selSimulationQuarge(HttpServletRequest request, @RequestParam(value = "fnumber", defaultValue = "") String fnumber
            , @RequestParam(value = "fdescription", defaultValue = "") String fdescription
			, @RequestParam(value = "studentName", defaultValue = "") String studentName
            , @RequestParam(value = "beginTime", defaultValue = "") String beginTime
            , @RequestParam(value = "endTime", defaultValue = "") String endTime
			, @RequestParam(value = "type",defaultValue = "") String type
			, @RequestParam(value = "selectState",required = false,defaultValue = "-1") String selectState)throws Exception{
		PageBean<SimulationQuarge> pb = new PageBean<>();
        //获取操作人信息
        User user = (User) request.getSession().getAttribute("user");
        try {
          
            fnumber = fnumber.trim();//去空格
            fdescription = fdescription.trim();
            beginTime=beginTime.trim();
            endTime=endTime.trim();
			studentName=studentName.trim();
			if("".equals(studentName)){
				studentName =null;
			}
			if("1".equals(selectState)){//如果传过来的标记为1 所以业务日期为空默认查询全部
				pb = getPageBean(request, user.getFcompanyid(), fnumber,fdescription,null,null);
			}else{
				//默认时间
				if ("".equals(beginTime) && "".equals(endTime)) {
					beginTime = TimeFormat.getMonth();
					endTime = TimeFormat.getNowTime();
				}
				//获取分页
				pb = getPageBean(request, user.getFcompanyid(), fnumber,fdescription,beginTime,endTime);
			}
			//计数器
			int i = 0;
			//缓存需要删除的序列
			List<Integer> tem = new ArrayList<>();
            //获取当前分页的值
            List<SimulationQuarge> simulationQuargeList = pb.getBeanList();          
            //把获取到的数据传回页面
            for (SimulationQuarge simulationQuarge : simulationQuargeList) {
                //查询对应学员的信息
            	simulationQuarge.setSimulationQuargentryList(simulationQuargentryService.selectSimulationQuargentryByParentid(simulationQuarge.getFid(),studentName));
				i++;
				//如果子类为空则记住该序列序号
				if (simulationQuarge.getSimulationQuargentryList().size() ==0) {
					tem.add(i);
				}
            }
			//进行集合删除
			for (int j = 0; j < tem.size(); j++) {
				simulationQuargeList.remove(tem.get(j) - j - 1);
			}
			//生成信息总数
			String messageAmount="共有"+pb.getTr()+"条信息";
            request.setAttribute("messageAmount",messageAmount);
            request.setAttribute("fnumber", fnumber);
            request.setAttribute("fdescription", fdescription);
            request.setAttribute("studentName",studentName);
            request.setAttribute("beginTime", beginTime);
            request.setAttribute("endTime", endTime);
            request.setAttribute("simulationQuargeList", simulationQuargeList);
            request.setAttribute("pb", pb);
            request.setAttribute("type",type);
			request.setAttribute("selectState",selectState);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }		
        return "backHome/examManage/simulationQuarge/simulationQuargeList";
	}
	/**
	 * 跳转到增加模拟考合格登记
	 * @param request
	 * @param fid
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/intoAddSimulationQuarge")
	public String intoAddSimulationQuarge(HttpServletRequest request,String fid,String type)throws Exception{
		//获取操作人信息
	    User user = (User)request.getSession().getAttribute("user");
	    //获取部门
	    //List<Department>departments = departmentService.queryAdminByController(user.getFcompanyid());
	    //获取组织
	    Department department = departmentService.queryAdminByFid(user.getFcompanyid());
	    //获取上一个编码
        /*String number = simulationQuargeService.selectFnumber(user.getFcompanyid());*/
        //获取编码
        String fnumber = GetFnumber.fnumbers("MN");
        //获取当前时间
	    Calendar now = Calendar.getInstance();
	    //获取年
	    int year=now.get(Calendar.YEAR);
	    //获取月
	    int month=now.get(Calendar.MONTH) + 1;
	    //获取日
	    int day=now.get(Calendar.DAY_OF_MONTH);
	   	now.add(Calendar.MONTH, -3);// 月份减1
	    /*获取当前时间*/
        String createTime = TimeFormat.timeFormat(new Date());
        Date resultDate = now.getTime(); // 结果
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		//业务时间
		String time = sdf.format(new Date());
        String beginTime = sdf.format(resultDate);
        String endTime = sdf.format(new Date());
        Map map = getPageBean2(request, "", "", "0", beginTime, endTime);
        request.setAttribute("beginTime", beginTime);
        request.setAttribute("endTime", endTime);
        request.setAttribute("pb", map.get("pb"));
        request.setAttribute("students", map.get("sList"));
	    request.setAttribute("time",time);
        request.setAttribute("fnumber", fnumber);
        request.setAttribute("department", department);
        request.setAttribute("user", user);
        request.setAttribute("createTime", createTime);
        request.setAttribute("type",type);
		return"backHome/examManage/simulationQuarge/addSimulationQuarge";
	}
	/**
	 * 增加模拟考试合格登记信息
	 * @param request
	 * @param simulationQuarge
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/addSimulationQuarge")
	public String addSimulationQuarge(HttpServletRequest request,SimulationQuarge simulationQuarge)throws Exception{
		//String[] sfid = request.getParameterValues("sfid");
		String[] sfseq = request.getParameterValues("sfseq");
        String[] sstuid = request.getParameterValues("sstuid");
        String[] stuname=request.getParameterValues("stuname");
        String[] scfidcardno = request.getParameterValues("scfidcardno");
        String[] scfsex = request.getParameterValues("scfsex");
        String[] cfsjhm=request.getParameterValues("cfsjhm");
        if(scfsex!=null){
	        for (int i=0;i<scfsex.length;i++) {
				if(scfsex[i].equals("男")){
					scfsex[i]="1";
				}else{
					scfsex[i]="2";
				}
			}
        }
        String[] scfgrade = request.getParameterValues("scfgrade");
        if(scfgrade != null){
			for (int i = 0; i < scfgrade.length; i++) {
				if("".equals(scfgrade[i])){
					scfgrade[i] = "0";
				}
			}
		}
		String[] scfremark = request.getParameterValues("scfremark");
       /*获取uuid*/
        String uuid = GetFid.UUIDIntoFid();
        //获取操作人信息
        User user = (User) request.getSession().getAttribute("user");
        simulationQuarge.setFid(uuid);
        //公司id
        simulationQuarge.setCfficompanyid(user.getFcompanyid());
        //控制单元id
        simulationQuarge.setFcontrolunitid(user.getFcompanyid());
        simulationQuarge.setFcreatorid(user.getFid());
        //增加主表
		int row1=0;
		int row2=0;
        row1=simulationQuargeService.addSimulationQuarge(simulationQuarge);
        //增加子表
        for (int i = 0; i < sstuid.length; i++) {
        	SimulationQuargentry  simulationQuargentry=new SimulationQuargentry();
            simulationQuargentry.setFid(GetFid.UUIDIntoFid());
            simulationQuargentry.setFparentid(uuid);
            simulationQuargentry.setFseq(Long.valueOf(sfseq[i]));
            simulationQuargentry.setStudentName(stuname[i]);
            simulationQuargentry.setCfstudentid(sstuid[i]);
            simulationQuargentry.setCfidcardno(scfidcardno[i]);
            simulationQuargentry.setCfsex(Long.valueOf(scfsex[i]));
            simulationQuargentry.setCfgrade(Long.valueOf(scfgrade[i]));
            simulationQuargentry.setCfremark(scfremark[i]);
            simulationQuargentry.setCfmobile(cfsjhm[i]);
            row2+=simulationQuargentryService.addSimulationQuargentry(simulationQuargentry);
			//添加学员的服务日志
			customLogService.addLog("考试管理", TimeFormat.timeFormat(new Date()) + "新增了学员"+simulationQuargentry.getStudentName()+"模拟考合格登记!成绩:"+simulationQuargentry.getCfgrade(),
					simulationQuargentry.getCfstudentid(), user.getFid());
        }
        String result="";
        if(row1>0 &&row2>0){
			result="1";
		}
        //添加日志
        customLogService.addLog("考试管理", TimeFormat.timeFormat(new Date()) + "模拟考合格登记进行增加", null, user.getFid());
		return "redirect:/DrivingSys/intoAddSimulationQuarge?type="+result;
	}
	/**
	 * 跳转到修改模拟考合格登记页面
	 * @param request
	 * @param fid
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/intoUpdateSimulationQuarge")
	public String intoUpdateSimulationQuarge(HttpServletRequest request,
			@RequestParam(value="fid",required=false) String fid)throws Exception{
		//获取操作人信息
        User user = (User) request.getSession().getAttribute("user");
        //获取组织
	    Department department = departmentService.queryAdminByFid(user.getFcompanyid());
        //获取父类模拟考合格登记信息
        SimulationQuarge simulationQuarge =simulationQuargeService.selectSimulationQuargeByFid(fid);
        if(simulationQuarge == null){
        	return "/error/not_data";//单据不存在则跳转到错误页面
		}
        //获取子类信息
        simulationQuarge.setSimulationQuargentryList(simulationQuargentryService.selectSimulationQuargentryByParentid(fid,null));
        //获取当前时间
	    Calendar now = Calendar.getInstance();
	   	now.add(Calendar.MONTH, -3);// 月份减1
	    /*获取当前时间*/
        Date resultDate = now.getTime(); // 结果
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String beginTime = sdf.format(resultDate);
        String endTime = sdf.format(new Date());
        Map map = getPageBean2(request, "", "", "0", beginTime, endTime);
        request.setAttribute("beginTime", beginTime);
        request.setAttribute("endTime", endTime);
        request.setAttribute("pb", map.get("pb"));
        request.setAttribute("students", map.get("sList"));
        request.setAttribute("simulationQuarge", simulationQuarge);
		request.setAttribute("department",department);
        return "backHome/examManage/simulationQuarge/updateSimulationQuarge";
	}
	/**
	 * 修改模拟考试合格登记信息
	 * @param request
	 * @param simulationQuarge
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/updateSimulationQuarge")
	public String updateSimulationQuarge(HttpServletRequest request,SimulationQuarge simulationQuarge)throws Exception{
		//获取操作人信息
        User user = (User) request.getSession().getAttribute("user");
        String[] sfseq = request.getParameterValues("sfseq");
        String[] sstuid = request.getParameterValues("sstuid");
        String[] stuname=request.getParameterValues("stuname");
        String[] scfidcardno = request.getParameterValues("scfidcardno");
        String[] scfsex = request.getParameterValues("scfsex");
        String[] cfsjhm=request.getParameterValues("cfsjhm");
        if(scfsex!=null){
	        for (int i=0;i<scfsex.length;i++) {
				if(scfsex[i].equals("男")){
					scfsex[i]="1";
				}else{
					scfsex[i]="2";
				}
			}
        }
        String[] scfgrade = request.getParameterValues("scfgrade");
		if(scfgrade != null){
			for (int i = 0; i < scfgrade.length; i++) {
				if("".equals(scfgrade[i])){
					scfgrade[i] = "0";
				}
			}
		}
        String[] scfremark = request.getParameterValues("scfremark");
        /*if (simulationQuarge.getCfbizstate()==5L) {//如何状态为5则是审计
        	simulationQuarge.setFauditorid(user.getFid());//设置审计人id       	
        }*/
        //把创建时间和业务日期清空不让修改
        simulationQuarge.setFcreatetime(null);
        //simulationQuarge.setFbizdate(null);
        //修改
        int row1 = simulationQuargeService.updateSimulationQuarge(simulationQuarge);
        //根据父类Id删除子类的明细信息
        if(simulationQuarge!=null){
        	 simulationQuargentryService.deleteByParentId(simulationQuarge.getFid(),null);
        }
        //根据父类id增加模拟考试合格登记信息明细
		int row2=0;
        for(int i=0;i<sstuid.length;i++){
        	SimulationQuargentry  simulationQuargentry=new SimulationQuargentry();
            simulationQuargentry.setFid(GetFid.UUIDIntoFid());
            simulationQuargentry.setFparentid(simulationQuarge.getFid());
            simulationQuargentry.setFseq(Long.valueOf(sfseq[i]));
            simulationQuargentry.setStudentName(stuname[i]);
            simulationQuargentry.setCfstudentid(sstuid[i]);
            simulationQuargentry.setCfidcardno(scfidcardno[i]);
            simulationQuargentry.setCfsex(Long.valueOf(scfsex[i]));
            simulationQuargentry.setCfgrade(Long.valueOf(scfgrade[i]).longValue());
            simulationQuargentry.setCfremark(scfremark[i]);
            simulationQuargentry.setCfmobile(cfsjhm[i]);
            row2 += simulationQuargentryService.addSimulationQuargentry(simulationQuargentry);
			//添加学员的服务日志
			customLogService.addLog("考试管理", TimeFormat.timeFormat(new Date()) + "修改了学员"+simulationQuargentry.getStudentName()+"模拟考合格登记!成绩:"+simulationQuargentry.getCfgrade(),
					simulationQuargentry.getCfstudentid(), user.getFid());
        }
        String result = "";
        if(row1 > 0 && row2 >0){
			result ="1";
		}
        //添加日志
        customLogService.addLog("考试管理", TimeFormat.timeFormat(new Date()) + "模拟考合格登记进行修改", null, user.getFid());
		return "redirect:/DrivingSys/querySimulationQuarge/look.action?type="+result;
	}
	/**
	 * 根据fid删除模拟考合格登记信息
	 * @param request
	 * @param deleteId
	 * @param fid
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/deleteSimulationQuargeByFid")
	public String deleteSimulationQuarge(HttpServletRequest request, String[] deleteId,String fid)throws Exception{
		User user = (User) request.getSession().getAttribute("user");
        //删除记录
		if(fid==null || fid.equals("")){
			for (int i = 0; i < deleteId.length; i++) {
	            /*过滤为空的删除操作*/
	            if (!deleteId[i].equals("")&&deleteId[i]!=null) {
	            	//根据fid获取单据编号
					SimulationQuarge simulationQuarge =simulationQuargeService.selectSimulationQuargeByFid(deleteId[i]);
					//删除历史流程或者正在进行的流程
					activitiUtil.deleteTask(ActivitiKey.PUBLIC_KEY,simulationQuarge.getFnumber());
	            	simulationQuargeService.deleteByPrimaryKey(deleteId[i]);//删除父类
	            	simulationQuargentryService.deleteByParentId(deleteId[i],user);//删除子类
	            }
	        }	
		}else{
			//根据fid获取单据编号
			SimulationQuarge simulationQuarge =simulationQuargeService.selectSimulationQuargeByFid(fid);
			//删除历史流程或者正在进行的流程
			activitiUtil.deleteTask(ActivitiKey.PUBLIC_KEY,simulationQuarge.getFnumber());
			simulationQuargeService.deleteByPrimaryKey(fid);//删除父类
			simulationQuargentryService.deleteByParentId(fid,user);//删除子类
        }
		//添加日志
        customLogService.addLog("考试管理", TimeFormat.timeFormat(new Date()) + "模拟考合格登记进行删除", null, user.getFid());
		return"redirect:/DrivingSys/querySimulationQuarge/look.action";
	}

	/**
	 * 提交模拟考试登记单据
	 * @param fid
	 * @param request
	 * @param fnumber
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "submitSimulationQuargeActiviti")
	@ResponseBody
	public Object submitSimulationQuargeActiviti(String fid, HttpServletRequest request, String fnumber)throws  Exception{
		//获取操作人信息
		User user = (User) request.getSession().getAttribute("user");
		SimulationQuarge simulationQuarge =new SimulationQuarge();
		simulationQuarge.setFid(fid);
		simulationQuarge.setCfbizstate(4L);
		simulationQuarge.setFlastupdateuserid(user.getFid());
		//创建流程
		axamine.submissionProcess(fnumber);
		int row=simulationQuargeService.updateSimulationQuarge(simulationQuarge);
		//根据父类id查找子类信息
		List<SimulationQuargentry> simulationQuargentries= simulationQuargentryService.selectSimulationQuargentryByParentid(fid,null);
		if(simulationQuargentries != null){
			for (int i = 0; i < simulationQuargentries.size(); i++) {
				//添加学员的服务日志
				customLogService.addLog("考试管理", TimeFormat.timeFormat(new Date()) + "提交了学员"+simulationQuargentries.get(i).getStudentName()+"模拟考合格登记!成绩:"+simulationQuargentries.get(i).getCfgrade(),
						simulationQuargentries.get(i).getCfstudentid(), user.getFid());
			}
		}
		String result="failed";
		if(row >0){
			result="success";
		}
		Map<String,String> _map =new HashMap<>();
		_map.put("result",result);
		return _map;
	}

	/**
	 * 审核单据
	 * @param request
	 * @param fid
	 * @param agree
	 * @param auditcomment
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "checkSimulationQuargeActiviti")
	@ResponseBody
	public Object checkSimulationQuargeActiviti(HttpServletRequest request,String fnumber,String fid,String agree,String auditcomment)throws  Exception{
		//获取操作人信息
		User user = (User)request.getSession().getAttribute("user");
		SimulationQuarge simulationQuarge =new SimulationQuarge();
		//获取当前修改时间
		String updateTime = TimeFormat.timeFormat(new Date());
		String result ="failed";
		int row1=0;
		if(agree.equals("1")){//同意
			ServerResponse serverResponse = axamine.auditProcess(fnumber, agree, auditcomment);
			if(serverResponse.getStatus() == 1){//结束
				simulationQuarge.setFid(fid);
				simulationQuarge.setCfbizstate(5L);
				simulationQuarge.setFlastupdateuserid(user.getFid());
				simulationQuarge.setFauditorid(user.getFid());
				row1 = simulationQuargeService.updateSimulationQuarge(simulationQuarge);
				//根据父类id查找子类信息
				List<SimulationQuargentry> simulationQuargentries= simulationQuargentryService.selectSimulationQuargentryByParentid(fid,null);
				if(simulationQuargentries != null){
					for (int i = 0; i < simulationQuargentries.size(); i++) {
						//添加学员的服务日志
						customLogService.addLog("考试管理", TimeFormat.timeFormat(new Date()) + "审核了学员"+simulationQuargentries.get(i).getStudentName()+"模拟考合格登记!成绩:"+simulationQuargentries.get(i).getCfgrade(),
								simulationQuargentries.get(i).getCfstudentid(), user.getFid());
					}
				}
				if(row1 >0 ){//修改单据和修改学员都成功
					result = "success";
				}
			}else if(serverResponse.getStatus() == 2){
				result = "success";
			}else{}
		}else{
			ServerResponse serverResponse = axamine.auditProcess(fnumber, agree, auditcomment);
			if(serverResponse.getStatus() == 2){
				simulationQuarge.setFid(fid);
				simulationQuarge.setCfbizstate(3L);
				simulationQuarge.setFlastupdateuserid(user.getFid());
				simulationQuarge.setFauditorid(user.getFid());
				//修改父类
				row1 =simulationQuargeService.updateSimulationQuarge(simulationQuarge);
				if(row1 >0){
					result = "success";
				}
			}
		}
		Map<String,String> _map =new HashMap<>();
		_map.put("result",result);
		return _map;
	}

	/**
	 * 反审核模拟考试结果单据
	 * @param request
	 * @param fid
	 * @param againstAuditcomment
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "againstCheckSimulationQuargeActiviti")
	@ResponseBody
	public Object againstCheckSimulationQuargeActiviti(HttpServletRequest request,String fid,String fnumber,String againstAuditcomment )throws  Exception{
		//获取当前登录用户的信息
		User user =(User)request.getSession().getAttribute("user");
		String result ="falied";
		//将单据状态修改成保存状态3
		SimulationQuarge simulationQuarge = new SimulationQuarge();
		simulationQuarge.setFid(fid);
		simulationQuarge.setCfbizstate(3L);
		simulationQuarge.setFlastupdateuserid(user.getFid());
		simulationQuarge.setFauditorid(user.getFid());
		int row = simulationQuargeService.updateSimulationQuarge(simulationQuarge);
		axamine.againstAuditProcess(fnumber,againstAuditcomment);
		if (row > 0) {
			result = "success";
		}
		Map<String,String> _map =new HashMap<>();
		_map.put("result",result);
		return _map;
	}


	
	@SuppressWarnings("unchecked")
    /**
     * 返回PageBean实体类  包括记录总数，页码，每页记录集等  dmz 20160921
     * @return 返回PageBean实体类
     * @throws Exception
     */
    public PageBean<SimulationQuarge> getPageBean(HttpServletRequest request, String companyId, String fnumber,String fdescription,String beginTime,String endTime) throws Exception {

        int offset;//从第几条数据开始加载
        if (request.getParameter("pager.offset") == null) {
            offset = 0;
        } else {
            offset = Integer.parseInt(request.getParameter("pager.offset"));
        }
        //拼接SQL语句
        StringBuffer psql = new StringBuffer();       
        psql.append(" SELECT R.fid, R.CFCARBYTE, R.FAUDITORID, R.FNUMBER, R.CFBIZSTATE,TO_CHAR(R.FBIZDATE, 'YYYY-MM-DD ') FBIZDATE,R.FDESCRIPTION, u.NAME AS checkName, e.NAME AS createName " +
                     " FROM CT_DS_SIMULATIONQUAREG R LEFT JOIN T_E_USER e on R.FCREATORID=e.fid  LEFT JOIN T_E_USER u  on R.FAUDITORID=u.fid ");
        psql.append(" where FCONTROLUNITID= '" + companyId + "' ");
         
        if (fnumber == null || fnumber.equals("")) {
			
		} else {
			psql.append( "AND R.FNUMBER LIKE '%" + fnumber + "%' ");
		}
		if (fdescription == null || fdescription.equals("")) {

		} else {
			psql.append( "AND R.FDESCRIPTION LIKE '%" + fdescription + "%'");
		}
		if (beginTime == null || endTime == null) {
		/*	 beginTime = TimeFormat.getMonth();
		} else if(endTime == null || endTime.equals("")){				 	
		        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");			      
		        endTime = sdf.format(new Date());
			psql.append("AND TO_CHAR(R.FBIZDATE,'yyyy-MM-dd') BETWEEN '" + beginTime + "' AND '" + endTime + "'"); */
		}else{
			psql.append("AND TO_CHAR(R.FBIZDATE,'yyyy-MM-dd') BETWEEN '" + beginTime + "' AND '" + endTime + "'"); 
		}
		
		psql.append(" ORDER BY R.FBIZDATE DESC");
		
        int psize = 10; //每页显示数
        int pindex = offset / psize;//+1  页码从0开始
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap = simulationQuargeService.selectPage(pindex, psql.toString(), psize);
        PageBean<SimulationQuarge> pb = new PageBean<>();
        pb.setPc(pindex); //页码
        Map<String, Object> paramMapCount = getPageBeanCount(psql.toString()); //取记录总数
        pb.setTr((int) paramMapCount.get("Pcount")); //记录总数
        pb.setPs(psize);
        pb.setBeanList((List<SimulationQuarge>) paramMap.get("v_cur")); //每页数据集
        /*System.out.println(psql);*/
        return pb;
    }
	
	/**
	 * @Author lcx
	 * @Date 2017/11/16 0016 上午 8:47
	 * 档案管理查询学生信息
	 */
	@SuppressWarnings("unchecked")
	@ResponseBody
	@RequestMapping(value = "/simulationQuargeGetStudent")
	public Map getPageBean2(HttpServletRequest request, @RequestParam(defaultValue = "") String name, @RequestParam(defaultValue = "") String cflxdh, @RequestParam(defaultValue = "1") String pindex, String beginTime, String endTime) throws Exception {	
		String psql = "SELECT S.FID, S.FNAME_L2, S.FNUMBER, S.FSIMPLENAME, S.FDESCRIPTION_L2, " +
				" U.NAME CREATENAME, " +
				" TO_CHAR(S.FLASTUPDATETIME, 'YYYY-MM-DD HH:MI:SS') FLASTUPDATETIME,S.CFSTUDENTSTATE,TO_CHAR(S.CFRXSJ, 'YYYY-MM-DD ') CFRXSJ, " +
				" S.CFRECEIVED,S.CFFAVORABLE,S.CFARREARAGE,S.CFBXWX,S.CFPINYIN, S.CFKMYKSS ,S.CFKMEKSS ,S.CFKMSKSS ,"+
				" TO_CHAR(S.CFKMYHERQ, 'YYYY-MM-DD ') CFKMYHERQ, TO_CHAR(S.CFKMEHGRQ, 'YYYY-MM-DD ') CFKMEHGRQ, TO_CHAR(S.CFKMSHGRQ, 'YYYY-MM-DD ') CFKMSHGRQ, TO_CHAR(S.CFKMYBJGSJ, 'YYYY-MM-DD ') CFKMYBJGSJ, TO_CHAR(S.CFKMSBJGSJ, 'YYYY-MM-DD ')CFKMSBJGSJ, TO_CHAR(S.CFKMEBJGSJ, 'YYYY-MM-DD ') CFKMEBJGSJ,"+
				" N.NAME UPDATENAME,A.FNAME_L2 as cfficompanyName,S.CFIDCARDNO ,TO_CHAR(S.CFBIRTHDAY, 'YYYY-MM-DD') CFBIRTHDAYS, " +
				" S.CFADDRESS,S.CFLINKADDRESS,S.CFSJHM,S.CFLXDH,S.CFSLLX,S.CFXZJCXDH, s.CFCFWZ, s.CFZZZH, " +
				" S.CFSLZJCXDH,TO_CHAR(S.FCREATETIME, 'YYYY-MM-DD') FCREATETIME,U2.FNAME_L2 AS CFTJRNAME,C.FNAME_L2 AS CFCLAZZNAME,S.CFAMOUNT " +
				" FROM CT_DS_STUDENT S " +
				" LEFT JOIN T_E_USER U ON S.FCREATORID = U.FID " +
				" LEFT JOIN T_E_USER N ON S.FLASTUPDATEUSERID = N.FID " +
				" LEFT JOIN CT_DS_ADMIN A ON S.FCONTROLUNITID = A.FID " +
				" LEFT JOIN CT_DS_PERSON  U2 ON S.CFTJRID=U2.FID " +
				" LEFT JOIN CT_DS_CLAZZ C ON S.CFCLAZZID=C.FID" +
				" where S.FNAME_L2 like '%" + name + "%' and s.cflxdh like '%" + cflxdh + "%' ";
		if (beginTime != null && endTime != null) {
			psql += " and to_char(s.CFRXSJ,'yyyy-MM-dd') BETWEEN '" + beginTime + "' AND '" + endTime + "'";
					/*" order by s.FLASTUPDATETIME desc"*/
		}
		int psize = 10; //每页显示数
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap = studentService.selectPage(Integer.valueOf(pindex), psql, psize);
		PageBean<Student> pb = new PageBean<>();
		pb.setPc(Integer.valueOf(pindex)); //页码
		Map<String, Object> paramMapCount = getPageBeanCount(psql); //取记录总数
		pb.setTr((int) paramMapCount.get("Pcount")); //记录总数
		pb.setPs(psize);
		Map stu = new HashMap();
		stu.put("sList", (List<Student>) paramMap.get("v_cur"));

		stu.put("pb", (pb.getTr() / pb.getPs()));

		return stu; //每页数据集;

	}
	/**
	 * 取记录总数 dmz 20160921
	 * @param psql  SQL语句
	 * @return Map
	 * @throws Exception
	 */
	public Map<String, Object> getPageBeanCount(String psql) throws Exception
	{

		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap=studentService.selectPageCount(psql);
		return paramMap;
	}

	/**
	 * 模拟考合格登记 导出excel
	 * @param response
	 * @param request
	 * @param fnumber
	 * @param fdescription
	 * @param studentName
	 * @param beginTime
	 * @param endTime
	 */
	@RequestMapping(value = "/importSimulationQuarge")
	public void importSimulationQuarge(HttpServletResponse response, HttpServletRequest request
			, @RequestParam(value = "fnumber", defaultValue = "") String fnumber
			, @RequestParam(value = "fdescription", defaultValue = "") String fdescription
			, @RequestParam(value = "studentName", defaultValue = "") String studentName
			, @RequestParam(value = "beginTime", defaultValue = "") String beginTime
			, @RequestParam(value = "endTime", defaultValue = "") String endTime){
		if("".equals(studentName)){
			studentName =null;
		}
		//获取操作人信息
		User user = (User) request.getSession().getAttribute("user");
		//定义接收类型
		List<Object> data = new ArrayList<>();
		//查询数据
		List<SimulationQuarge> simulationQuarges=simulationQuargeService.selectSimulationQuarge(user.getFcompanyid(),fnumber,fdescription,beginTime,endTime);
		for (SimulationQuarge simulationQuarge:simulationQuarges){
			simulationQuarge.setSimulationQuargentryList(simulationQuargentryService.selectSimulationQuargentryByParentid(simulationQuarge.getFid(),studentName));
			for (int i = 0; i <simulationQuarge.getSimulationQuargentryList().size() ; i++) {
				data.add(simulationQuarge.getFnumber());
				data.add(simulationQuarge.getFbizdate());
				data.add(simulationQuarge.getFdescription());
				data.add(simulationQuarge.getCheckName());
				data.add(simulationQuarge.getCreateName());
				if (simulationQuarge.getCfbizstate() == 3L) {
					data.add("保存");
				} else if (simulationQuarge.getCfbizstate() == 4L) {
					data.add("提交");
				} else {
					data.add("审核");
				}
				data.add(simulationQuarge.getSimulationQuargentryList().get(i).getStudentName());
				data.add(simulationQuarge.getSimulationQuargentryList().get(i).getCfidcardno());
				if(simulationQuarge.getSimulationQuargentryList().get(i).getCfsex() == 1L){
					data.add("男");
				}else if(simulationQuarge.getSimulationQuargentryList().get(i).getCfsex() == 2L){
					data.add("女");
				}else{
					data.add(null);
				}
				data.add(simulationQuarge.getSimulationQuargentryList().get(i).getCfmobile());
				data.add(simulationQuarge.getSimulationQuargentryList().get(i).getCfgrade());
				data.add(simulationQuarge.getSimulationQuargentryList().get(i).getCfremark());
			}
		}
		//头部数据
		String head[] = {"单据编号", "业务日期", "参考信息", "审核人名称", "制单人名称", "单据状态", "学员", "身份证号码","性别","手机号码","考试分数","备注"};
		//长度
		Integer length[] = {9000, 5000, 5000, 3000, 3000, 3000, 3000, 9000,3000,5000,5000,3000,5000};
		try {
			//导出Excel
			CommonExport.importExcel(response, "模拟考合格登记" + TimeFormat.getNowTime2() + ".xls", "模拟考合格登记", data, head, length);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
